<?php
/*
 * This is a php implementation of autoproxy2pac
 */


function reg_encode($str) {
	$tmp_str=$str;
	$tmp_str=str_replace('/', "\\/", $tmp_str);
	$tmp_str=str_replace('.', "\\.", $tmp_str);
	$tmp_str=str_replace(':', "\\:", $tmp_str);
	$tmp_str=str_replace('%', "\\%", $tmp_str);
	$tmp_str=str_replace('*', ".*", $tmp_str);
	$tmp_str=str_replace('-', "\\-", $tmp_str);
	$tmp_str=str_replace('&', "\\&", $tmp_str);
	$tmp_str=str_replace('?', "\\?", $tmp_str);
	
	return $tmp_str;
}

if((!isset($_GET['raw']))&&(!isset($_GET['rulelist']))&&(empty($_GET['type'])||empty($_GET['host'])||empty($_GET['port'])))
{
	echo '
		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
		<html xmlns="http://www.w3.org/1999/xhtml">
		<head profile="http://gmpg.org/xfn/11">
			<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
			<title>PTP AutoProxy2PAC</title>
			<style type="text/css">
				body {
					color:#333333;
					font:0.75em Arial,sans-serif;
					text-align:center;
				}
			</style>
		</head>
		<body>
			<div>
				<h3>Setup your pac</h3>
				<form action="" method="get" style="margin:0px;display:inline">
					Proxy Server:
					<select name="type">
						<option value="http">HTTP</option>
						<option value="socks">SOCKS</option>
					</select>
					<input name="host" type="text" size="16" value="127.0.0.1" />
					:
					<input name="port" type="text" size="5" value="8080" />
					<input type="submit" value="Get PAC" />
				</form>
				<form action="" method="get" style="margin:0px;display:inline">
					<input name="raw" type="submit" value="Get GFWLIST RAW" />
				</form>
				<form action="" method="get" style="margin:0px;display:inline">
					<input name="rulelist" type="submit" value="Get GFWLIST RULE" />
				</form>
			</div>
		</body>
		';
	
	
	exit(0);

}

if($_GET['type']=='http')
	$type='PROXY';
else if ($_GET['type']=='socks')
	$type='SOCKS5';
else
	$type=$_GET['type'];

$host=$_GET['host'];
$port=$_GET['port'];
$gfwlist_url='http://autoproxy-gfwlist.googlecode.com/svn/trunk/gfwlist.txt';

$ch=curl_init();  
curl_setopt($ch, CURLOPT_URL, $gfwlist_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$gfwlist_raw=curl_exec($ch);
curl_close($ch);
if(isset($_GET['raw']))
{
	header('Content-Type: text/plain');
	echo $gfwlist_raw;
	exit(0);
}
$rulelist=base64_decode($gfwlist_raw);
if(isset($_GET['rulelist']))
{
	echo '
		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
		<html xmlns="http://www.w3.org/1999/xhtml">
		<head profile="http://gmpg.org/xfn/11">
			<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
			<title>AutoProxy Rule List</title>
			<style type="text/css">
				body {
					color:#333333;
					font:1em Arial,sans-serif;
					//text-align:center;
				}
			</style>
			<script language="JavaScript">
				function showRuleList() {
					var rl = document.getElementById("rl");
					var oText = document.createTextNode(decode64("'.base64_encode($rulelist).'"));
					rl.appendChild(oText);
				}
				function decode64(_1){
					var _2="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
					var _3="";
					var _4,_5,_6;
					var _7,_8,_9,_a;
					var i=0;
					_1=_1.replace(/[^A-Za-z0-9\+\/\=]/g,"");
					do{
						_7=_2.indexOf(_1.charAt(i++));
						_8=_2.indexOf(_1.charAt(i++));
						_9=_2.indexOf(_1.charAt(i++));
						_a=_2.indexOf(_1.charAt(i++));
						_4=(_7<<2)|(_8>>4);
						_5=((_8&15)<<4)|(_9>>2);
						_6=((_9&3)<<6)|_a;
						_3=_3+String.fromCharCode(_4);
						if(_9!=64){_3=_3+String.fromCharCode(_5);}
						if(_a!=64){_3=_3+String.fromCharCode(_6);}
					}while(i<_1.length);
					return _3;
				}
			</script>
		</head>
		<body onload="showRuleList()">
			<div>
				<h3>AutoProxy Rule List</h3>
				<pre id="rl"></pre>
			</div>
		</body>
		</html>
	';
	//echo $rulelist;
	exit(0);
}
$gfwlist=explode("\n", $rulelist); 

$js_output='
//Generated by PTP Remote PAC
function FindProxyForURL(url, host) {
	var start = new Date();
	var PROXY = "'.$type.' '.$host.':'.$port.'";
	var DEFAULT = "DIRECT";
';
	
foreach($gfwlist as $index=>$rule){
	if(empty($rule))
		continue;
	else if(substr($rule,0,1)=='!' || substr($rule,0,1)=='[')
		continue;
	$return_proxy='PROXY';
	if(substr($rule,0,2)=='@@')
	{
		$rule=substr($rule,2);
		$return_proxy="DEFAULT";
	}
	
	if(substr($rule,0,2)=='||')
	{
		$rule_reg = "^[\\w\\-]+:\\/+(?!\\/)(?:[^\\/]+\\.)?".reg_encode(substr($rule,2));
	}
	else if(substr($rule,0,1)=='|')
	{
		$rule_reg = "^".reg_encode(substr($rule,1));
	}
	else if(substr($rule,0,1)=='/'&&substr($rule,-1)=='/')
	{
		$rule_reg = substr($rule,1,strlen($rule)-2);
	}
	else
	{
		$rule_reg=reg_encode($rule);
	}
	//echo '//'.$rule_reg."\n\t";
	$js_output.= "\t".'if(/'.$rule_reg.'/i.test(url)) return '.$return_proxy.';'."\n";
}
$js_output.="\t".'return DEFAULT;'."\n}\n";

if(!isset($_GET['no64']))
	$js_output='eval(decode64("'.base64_encode($js_output).'"));'."\n";

$js_output.='
function decode64(_1){
	var _2="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	var _3="";
	var _4,_5,_6;
	var _7,_8,_9,_a;
	var i=0;
	_1=_1.replace(/[^A-Za-z0-9\+\/\=]/g,"");
	do{
		_7=_2.indexOf(_1.charAt(i++));
		_8=_2.indexOf(_1.charAt(i++));
		_9=_2.indexOf(_1.charAt(i++));
		_a=_2.indexOf(_1.charAt(i++));
		_4=(_7<<2)|(_8>>4);
		_5=((_8&15)<<4)|(_9>>2);
		_6=((_9&3)<<6)|_a;
		_3=_3+String.fromCharCode(_4);
		if(_9!=64){_3=_3+String.fromCharCode(_5);}
		if(_a!=64){_3=_3+String.fromCharCode(_6);}
	}while(i<_1.length);
	return _3;
}
';

header('Content-Type: text/plain');
if(!isset($_GET['noCache']))
	header('Cache-Control:public, max-age=86400');
header('Content-Length: '.strlen($js_output));
echo $js_output;
?>
